package com.cwt.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cwt.service.domain.SysRole;
import com.cwt.service.domain.SysRoleMenu;
import com.cwt.service.service.SysRoleMenuService;
import com.cwt.service.service.SysRoleService;
import com.cwt.service.mapper.SysRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author Acer
* @description 针对表【sys_role(角色表)】的数据库操作Service实现
* @createDate 2023-07-19 11:55:16
*/
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
    implements SysRoleService{

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Override
    public SysRole querySysRoleByName(SysRole role) {
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        wrapper.eq("name", role.getName());
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public boolean insertRoleMenu(SysRole role) {
        List<SysRoleMenu> menus = new ArrayList<>();
        for (Integer menuId : role.getMenuIds()) {
            menus.add(new SysRoleMenu(role.getId(), menuId));
        }
        return sysRoleMenuService.saveBatch(menus);
    }

    @Override
    public boolean checkRoleNameUnique(SysRole role) {
        return querySysRoleByName(role).getName().equals(role.getName()); // true 唯一
    }

    @Override
    public boolean updateRoleMenu(SysRole role) {
        QueryWrapper<SysRoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("roleId", role.getId());
        Map<Integer, Integer> oldMenuMap = sysRoleMenuService.listMaps(wrapper).stream().collect(Collectors.toMap(key -> (Integer) key.get("menuId"), value -> (Integer) value.get("id")));
        List<Integer> oldMenus = new ArrayList<>(oldMenuMap.keySet());

        // 获取新添加的 菜单数据信息（旧菜单没有的），需要 新增
        List<Integer> insertMenu = role.getMenuIds().stream().filter(menu -> !oldMenus.contains(menu)).collect(Collectors.toList());

        // 获取新新菜单中没有的旧菜单，需要 删除
        List<Integer> deleteMenuKey = oldMenus.stream().filter(menu -> !Arrays.asList(role.getMenuIds()).contains(menu)).collect(Collectors.toList());

        // 最上班查询到的权限菜单数据中窜在 主键id，根据主键id进行删除，否侧会删除掉其他角色的相同菜单
        List<Integer> deleteMenu = new ArrayList<>();
        oldMenuMap.forEach((key, value) -> {
            if (deleteMenuKey.contains(key)) {
                deleteMenu.add(value);
            }
        });

        // 菜单删除、新增
        if (sysRoleMenuService.removeByIds(deleteMenu)) {
            List<SysRoleMenu> menus = new ArrayList<>();
            for (Integer menuId : insertMenu) {
                menus.add(new SysRoleMenu(role.getId(), menuId));
            }
            return sysRoleMenuService.saveBatch(menus);
        }
        return false;
    }

    @Override
    public List<SysRole> getUserRoles(Long userId) {
        return baseMapper.getUserRoles(userId);
    }
}




